{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "872bb8b5",
   "metadata": {},
   "source": [
    "# Transformation\n",
    "\n",
    "Often we want to transform inputs as they are passed from one component to another.\n",
    "\n",
    "As an example, we will create a dummy transformation that takes in a super long text, filters the text to only the first 3 paragraphs, and then passes that into a chain to summarize those."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d257f50d-c53d-41b7-be8a-df23fbd7c017",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.prompts import PromptTemplate\n",
    "\n",
    "prompt = PromptTemplate.from_template(\n",
    "    \"\"\"Summarize this text:\n",
    "\n",
    "{output_text}\n",
    "\n",
    "Summary:\"\"\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8ae5937c",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"../../state_of_the_union.txt\") as f:\n",
    "    state_of_the_union = f.read()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c938536-e3fb-45eb-a1b3-cb82be410e32",
   "metadata": {},
   "source": [
    "## Using LCEL\n",
    "\n",
    "With LCEL this is trivial, since we can add functions in any `RunnableSequence`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "1e53e851-b1bd-424f-a144-5f2e8b413dcf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The speaker acknowledges the presence of important figures in the government and addresses the audience as fellow Americans. They highlight the impact of COVID-19 on keeping people apart in the previous year but express joy in being able to come together again. The speaker emphasizes the unity of Democrats, Republicans, and Independents as Americans.'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.chat_models import ChatOpenAI\n",
    "from langchain.schema import StrOutputParser\n",
    "\n",
    "runnable = (\n",
    "    {\"output_text\": lambda text: \"\\n\\n\".join(text.split(\"\\n\\n\")[:3])}\n",
    "    | prompt\n",
    "    | ChatOpenAI()\n",
    "    | StrOutputParser()\n",
    ")\n",
    "runnable.invoke(state_of_the_union)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9b9bd07-155f-4777-9215-509d39ecfe3f",
   "metadata": {},
   "source": [
    "## [Legacy] TransformationChain\n",
    "\n",
    ":::note\n",
    "\n",
    "This is a legacy class, using LCEL as shown above is preferred.\n",
    "\n",
    ":::\n",
    "\n",
    "This notebook showcases using a generic transformation chain."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "bbbb4330",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chains import LLMChain, SimpleSequentialChain, TransformChain\n",
    "from langchain.llms import OpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "98739592",
   "metadata": {},
   "outputs": [],
   "source": [
    "def transform_func(inputs: dict) -> dict:\n",
    "    text = inputs[\"text\"]\n",
    "    shortened_text = \"\\n\\n\".join(text.split(\"\\n\\n\")[:3])\n",
    "    return {\"output_text\": shortened_text}\n",
    "\n",
    "\n",
    "transform_chain = TransformChain(\n",
    "    input_variables=[\"text\"], output_variables=[\"output_text\"], transform=transform_func\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "e9397934",
   "metadata": {},
   "outputs": [],
   "source": [
    "template = \"\"\"Summarize this text:\n",
    "\n",
    "{output_text}\n",
    "\n",
    "Summary:\"\"\"\n",
    "prompt = PromptTemplate(input_variables=[\"output_text\"], template=template)\n",
    "llm_chain = LLMChain(llm=OpenAI(), prompt=prompt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "06f51f17",
   "metadata": {},
   "outputs": [],
   "source": [
    "sequential_chain = SimpleSequentialChain(chains=[transform_chain, llm_chain])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "f7caa1ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "' In an address to the nation, the speaker acknowledges the hardships of the past year due to the COVID-19 pandemic, but emphasizes that regardless of political affiliation, all Americans can come together.'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sequential_chain.run(state_of_the_union)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
